package com.ytktt.study.week4.task2;

/**
 * wait-notify方式
 *
 * @author ytk
 */
public class WaitNotifyDemo {

    private static volatile int result = 0;
    private static Object o = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(() -> {
            result = sum();
            synchronized (o) {
                o.notify();
            }
        }, "wait-notify");
        t.start();
        synchronized (o) {
            o.wait();
        }
        System.out.println("异步计算结果为：" + result);
    }

    private static int sum() {
        return fibo(36);
    }

    private static int fibo(int a) {
        if (a < 2) {
            return 1;
        }
        return fibo(a - 1) + fibo(a - 2);
    }
}
